<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="80px">
      <el-form-item label="真实姓名" prop="teacherName">
        <el-input v-model="dataForm.teacherName" placeholder="真实姓名"></el-input>
      </el-form-item>
      <!-- <el-form-item label="登录用户名" prop="userName">
         <el-input v-model="dataForm.userName" placeholder="登录用户名"></el-input>
       </el-form-item>-->
      <el-form-item label="密码" prop="teacherPassword">
        <el-input v-model="dataForm.teacherPassword" placeholder="密码"></el-input>
      </el-form-item>
      <el-form-item label="职业名称" prop="professionName">
        <template>
          <el-select v-model="dataForm.professionId" placeholder="请选择">
            <el-option
              v-for="item in dataForm.professionList"
              :key="item.professionId"
              :label="item.professionName"
              :value="item.professionId">
            </el-option>
          </el-select>
        </template>
      </el-form-item>
      <el-form-item label="性别选择" prop="teacherGender">
        <template>
          <el-radio-group v-model="dataForm.teacherGender">
            <el-radio :label="1">男</el-radio>
            <el-radio :label="2">女</el-radio>
            <el-radio :label="3">保密</el-radio>
          </el-radio-group>
        </template>
      </el-form-item>
      <!--<el-form-item label="头像" prop="teacherHeader">
        <el-input v-model="dataForm.teacherHeader" placeholder="头像"></el-input>
      </el-form-item>-->
      <el-form-item label="手机号码" prop="teacherMobile">
        <el-input v-model="dataForm.teacherMobile" placeholder="手机号码"></el-input>
      </el-form-item>
      <!--<el-form-item label="状态：待审核（0），有效（1），无效（2）" prop="status">
        <el-input v-model="dataForm.status" placeholder="状态：待审核（0），有效（1），无效（2）"></el-input>
      </el-form-item>-->
      <el-form-item label="生日" prop="birthday">
        <el-date-picker
          v-model="dataForm.birthday"
          type="date"
          format="yyyy 年 MM 月 dd 日"
          value-format="yyyy-MM-dd"
          placeholder="生日">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="学历" prop="teacherEducation">
        <template>
          <el-select v-model="dataForm.teacherEducation" placeholder="请选择">
            <el-option
              v-for="item in dataForm.educationList"
              :key="item.dictId"
              :label="item.dictKey"
              :value="item.dictValue" >
            </el-option>
          </el-select>
        </template>
      </el-form-item>
      <el-form-item label="大学编码" prop="collegeCode">
        <el-input v-model="dataForm.collegeCode" placeholder="大学编码"></el-input>
      </el-form-item>
      <el-form-item label="大学名称" prop="collegeName">
        <el-input v-model="dataForm.collegeName" placeholder="大学名称"></el-input>
      </el-form-item>
      <el-form-item label="资格证书编号" prop="certNo">
        <el-input v-model="dataForm.certNo" placeholder="资格证书编号"></el-input>
      </el-form-item>
      <el-form-item label="签名" prop="teacherSign">
        <el-input v-model="dataForm.teacherSign" placeholder="签名"></el-input>
      </el-form-item>
      <el-form-item label="微信号" prop="teacherWechat">
        <el-input v-model="dataForm.teacherWechat" placeholder="微信号"></el-input>
      </el-form-item>
      <el-form-item label="qq号" prop="teacherQq">
        <el-input v-model="dataForm.teacherQq" placeholder="qq号"></el-input>
      </el-form-item>
      <!-- <el-form-item label="户籍所在省份" prop="provinceId">
         <el-input v-model="dataForm.provinceId" placeholder="户籍所在省份"></el-input>
       </el-form-item>
       <el-form-item label="户籍所在城市" prop="cityId">
         <el-input v-model="dataForm.cityId" placeholder="户籍所在城市"></el-input>
       </el-form-item>
       <el-form-item label="户籍所在地区" prop="areaId">
         <el-input v-model="dataForm.areaId" placeholder="户籍所在地区"></el-input>
       </el-form-item>-->
      <el-form-item label="籍贯" prop="censusRegister">
        <el-input v-model="dataForm.censusRegister" placeholder="籍贯"></el-input>
      </el-form-item>
      <el-form-item label="身份证" prop="idCard">
        <el-input v-model="dataForm.idCard" placeholder="身份证"></el-input>
      </el-form-item>
      <!--  <el-form-item label="现住住址" prop="currentAddress">
          <el-input v-model="dataForm.currentAddress" placeholder="现住住址"></el-input>
        </el-form-item>-->
      <!--<el-form-item label="最后一次登录IP" prop="loginIp">
        <el-input v-model="dataForm.loginIp" placeholder="最后一次登录IP"></el-input>
      </el-form-item>-->
      <!-- <el-form-item label="最后一次登录时间" prop="loginTime">
         <el-input v-model="dataForm.loginTime" placeholder="最后一次登录时间"></el-input>
       </el-form-item>-->
      <!--<el-form-item label="创建人" prop="createUser">
        <el-input v-model="dataForm.createUser" placeholder="创建人"></el-input>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
      </el-form-item>
      <el-form-item label="修改人" prop="updateUser">
        <el-input v-model="dataForm.updateUser" placeholder="修改人"></el-input>
      </el-form-item>
      <el-form-item label="修改时间" prop="updateTime">
        <el-input v-model="dataForm.updateTime" placeholder="修改时间"></el-input>
      </el-form-item>-->
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          teacherId: 0,
          teacherName: '',
          userName: '',
          teacherPassword: '',
          teacherGender: '',
          gender: '',
          teacherHeader: '',
          teacherMobile: '',
          status: '',
          birthday: '',
          teacherEducation: '',
          education: '',
          collegeCode: '',
          collegeName: '',
          certNo: '',
          teacherSign: '',
          teacherWechat: '',
          teacherQq: '',
          provinceId: '',
          cityId: '',
          areaId: '',
          censusRegister: '',
          idCard: '',
          currentAddress: '',
          loginIp: '',
          loginTime: '',
          createUser: '',
          createTime: '',
          updateUser: '',
          updateTime: '',
          professionId: 1,
          educationList: [],
          professionList: []
        },
        dataRule: {
          teacherName: [
            {required: true, message: '真实姓名不能为空', trigger: 'blur'}
          ],
          userName: [
            {required: true, message: '登录用户名不能为空', trigger: 'blur'}
          ]
          /* teacherPassword: [
           { required: true, message: '密码不能为空', trigger: 'blur' }
           ],
           teacherGender: [
           { required: true, message: '性别：1 男、2 女、3 未知不能为空', trigger: 'blur' }
           ],
           teacherHeader: [
           { required: true, message: '头像不能为空', trigger: 'blur' }
           ],
           teacherMobile: [
           { required: true, message: '手机号码不能为空', trigger: 'blur' }
           ],
           status: [
           { required: true, message: '状态：待审核（0），有效（1），无效（2）不能为空', trigger: 'blur' }
           ],
           birthday: [
           { required: true, message: '不能为空', trigger: 'blur' }
           ],
           teacherEducation: [
           { required: true, message: '学历：1 幼儿园、2 小学、3 初中、4 专科、5 本科、6 硕士、7 博士、8 博士后不能为空', trigger: 'blur' }
           ],
           collegeCode: [
           { required: true, message: '大学编码不能为空', trigger: 'blur' }
           ],
           collegeName: [
           { required: true, message: '大学名称不能为空', trigger: 'blur' }
           ],
           certNo: [
           { required: true, message: '资格证书编号不能为空', trigger: 'blur' }
           ],
           teacherSign: [
           { required: true, message: '签名不能为空', trigger: 'blur' }
           ],
           teacherWechat: [
           { required: true, message: '微信号不能为空', trigger: 'blur' }
           ],
           teacherQq: [
           { required: true, message: 'qq号不能为空', trigger: 'blur' }
           ],
           provinceId: [
           { required: true, message: '户籍所在省份不能为空', trigger: 'blur' }
           ],
           cityId: [
           { required: true, message: '户籍所在城市不能为空', trigger: 'blur' }
           ],
           areaId: [
           { required: true, message: '户籍所在地区不能为空', trigger: 'blur' }
           ],
           censusRegister: [
           { required: true, message: '籍贯不能为空', trigger: 'blur' }
           ],
           idCard: [
           { required: true, message: '身份证不能为空', trigger: 'blur' }
           ],
           currentAddress: [
           { required: true, message: '现住住址不能为空', trigger: 'blur' }
           ],
           loginIp: [
           { required: true, message: '最后一次登录IP不能为空', trigger: 'blur' }
           ],
           loginTime: [
           { required: true, message: '最后一次登录时间不能为空', trigger: 'blur' }
           ],
           createUser: [
           { required: true, message: '创建人不能为空', trigger: 'blur' }
           ],
           createTime: [
           { required: true, message: '创建时间不能为空', trigger: 'blur' }
           ],
           updateUser: [
           { required: true, message: '修改人不能为空', trigger: 'blur' }
           ],
           updateTime: [
           { required: true, message: '修改时间不能为空', trigger: 'blur' }
           ] */
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.teacherId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          // 获取学历信息
          this.$http({
            url: this.$http.adornUrl(`sys/dict/totalList`),
            method: 'get',
            params: this.$http.adornParams({
              dictType: 'sys_education'
            })
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.dataForm.professionName = ''
              this.dataForm.educationList = data.list
            }
          })
          // 获取职业名称列表
          this.$http({
            url: this.$http.adornUrl(`/operate/profession/totalList`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.dataForm.professionId = ''
              this.dataForm.professionList = data.list
            }
          })
          if (this.dataForm.teacherId) {
            this.$http({
              url: this.$http.adornUrl(`/operate/teacher/info/${this.dataForm.teacherId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.teacherName = data.teacher.teacherName
                this.dataForm.userName = data.teacher.userName
                this.dataForm.teacherPassword = data.teacher.teacherPassword
                this.dataForm.teacherGender = data.teacher.teacherGender
                this.dataForm.gender = data.teacher.gender
                this.dataForm.teacherHeader = data.teacher.teacherHeader
                this.dataForm.teacherMobile = data.teacher.teacherMobile
                this.dataForm.status = data.teacher.status
                this.dataForm.birthday = data.teacher.birthday
                this.dataForm.teacherEducation = data.teacher.teacherEducation.toString()
                this.dataForm.collegeCode = data.teacher.collegeCode
                this.dataForm.collegeName = data.teacher.collegeName
                this.dataForm.certNo = data.teacher.certNo
                this.dataForm.teacherSign = data.teacher.teacherSign
                this.dataForm.teacherWechat = data.teacher.teacherWechat
                this.dataForm.teacherQq = data.teacher.teacherQq
                this.dataForm.provinceId = data.teacher.provinceId
                this.dataForm.cityId = data.teacher.cityId
                this.dataForm.areaId = data.teacher.areaId
                this.dataForm.censusRegister = data.teacher.censusRegister
                this.dataForm.idCard = data.teacher.idCard
                this.dataForm.currentAddress = data.teacher.currentAddress
                this.dataForm.loginIp = data.teacher.loginIp
                this.dataForm.loginTime = data.teacher.loginTime
                this.dataForm.createUser = data.teacher.createUser
                this.dataForm.createTime = data.teacher.createTime
                this.dataForm.updateUser = data.teacher.updateUser
                this.dataForm.updateTime = data.teacher.updateTime
                this.dataForm.professionId = data.teacher.professionId
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/operate/teacher/${!this.dataForm.teacherId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'teacherId': this.dataForm.teacherId || undefined,
                'teacherName': this.dataForm.teacherName,
                'userName': this.dataForm.userName,
                'teacherPassword': this.dataForm.teacherPassword,
                'teacherGender': this.dataForm.teacherGender,
                'teacherHeader': this.dataForm.teacherHeader,
                'teacherMobile': this.dataForm.teacherMobile,
                'status': this.dataForm.status,
                'birthday': this.dataForm.birthday,
                'teacherEducation': this.dataForm.teacherEducation,
                'collegeCode': this.dataForm.collegeCode,
                'collegeName': this.dataForm.collegeName,
                'certNo': this.dataForm.certNo,
                'teacherSign': this.dataForm.teacherSign,
                'teacherWechat': this.dataForm.teacherWechat,
                'teacherQq': this.dataForm.teacherQq,
                'provinceId': this.dataForm.provinceId,
                'cityId': this.dataForm.cityId,
                'areaId': this.dataForm.areaId,
                'censusRegister': this.dataForm.censusRegister,
                'idCard': this.dataForm.idCard,
                'currentAddress': this.dataForm.currentAddress,
                'loginIp': this.dataForm.loginIp,
                'loginTime': this.dataForm.loginTime,
                'createUser': this.dataForm.createUser,
                'createTime': this.dataForm.createTime,
                'updateUser': this.dataForm.updateUser,
                'updateTime': this.dataForm.updateTime,
                'professionId': this.dataForm.professionId
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
